function ProductSelect_Code(flag) {
	var buffer = new StringBuffer();
	buffer.append('<div id="find2" class="finds2" flag="'+flag+'" style="z-index:100;"><div class="searchinput ui-border-radius"><input id="keyword2" class="bordersolid" type="text" value="" onblur="Show_Search2()" onkeyup="ProductSelect_Search();"/><div id="searchicon2" class="searchicon"><img src="img/jing.jpg"/></div><div id="searchicons2" class="searchicons"><img src="img/jings.jpg"/></div></div></div>');
	buffer.append('<div class="horizen border-bottom" style="z-index:100;"></div>');
	return buffer.toString();
};

function ProductSelect_Data(data) {
	var buffer = new StringBuffer();
	$(data).each(function(i,info){
		buffer.append('<div class="productlist border-bottom" id="'+info.ID+'" price="'+info.Price+'" unit="'+info.Unit+'"><div class="productname">'+info.Name+'</div><div class="productinfo"><div class="price">成本价：'+info.Price.toFixed(2)+'</div><div class="createdate">'+info.CreateDate.ToTime()+'</div></div></div>');
	});
	return buffer.toString();
};

function Receive_ProductSelect(pageindex)
{
	var data = {};
			data.act = "Find_Product";
			data.userid=userid;
			data.comid=comid;
			data.pageindex=pageindex;
			data.source=source;
			data.pagesize=PAGE_SIZE20;
			data.keyword = $("#keyword2").val();
		    data.timer = "";
		    data.state = "";
		    data.type = 1;
		    data.lowers = "";
		    data.sign=GetSign(data.act);
			
			mui.getJSON(crm_domain, data, function(json) {
				if(json.data.list.length>0)
				{
					if(pageindex==1)
					{
						$("#contentlist4").html(ProductSelect_Data(json.data.list));
					}else{
						$("#contentlist4").append(ProductSelect_Data(json.data.list));
					}
					setTimeout(function(){
						Init_ProductSelect();
						myScroll3.refresh();
					},500); //需要延迟500毫秒加载，否则当数量少时不能下拉
					if(json.data.list.length!=data.pagesize)
					{
						$("#loading4").html("");
					}else{
						$("#loading4").html('<img src="img/loading.gif"/>');
					}
				}else
				{
					if(pageindex==1)
					{
						$("#contentlist4").html("");
						$("#loading4").html("主人，没有找到您需要的数据"); 
					}else
					{
						$("#loading4").html("没有更多了");
					}
				}
				
         		pageindex = parseInt($("#divmsg4").attr("pageindex"))+1;
         		$("#divmsg4").attr("pageindex",pageindex);
         		
         		
			});
};

function ProductSelect_Search(){
	pageindex=1;
	$("#divmsg4").attr("pageindex","0");
	Receive_ProductSelect(1);
};




function Init_ProductSelect()
{
	mui("#contentlist4").off("tap",".productlist");
	mui("#contentlist4").on("tap",".productlist",function(){
		$("#product_tool font").hide();
		$("#product_tool table").removeClass("hidden");
		var flag=$("#find2").attr("flag");
		var o=$(this);
		if(flag=="order")
		{
			if($("#product_panel table tr[id="+$(this).attr("id")+"]").length>0){
				plus.nativeUI.toast("已选择了该产品");
				return;
			}
			$("#product_panel table").append('<tr id="'+$(this).attr("id")+'"><td width="30%" >'+$(this).find(".productname").html()+'</td><td width="10%">'+$(this).attr("unit")+'</td><td width="15%">'+$(this).attr("price").toFixed(2)+'</td><td width="15%"><div style="width:40px; height:100%; margin:0 auto;" class="ui-border-radius"><input type="tel" value="1" onkeyup="Check_Money(this);Cal_POrder(this)" onfocus="ScrollTop()" onclick="Last(this);"/></div></td><td width="15%"><div style="width:40px; height:100%; margin:0 auto;" class="ui-border-radius"><input type="tel" value="100" onkeyup="Check_Money(this);Cal_POrder(this)" onfocus="ScrollTop()" onclick="Last(this);"/></div></td><td width="15%">'+$(this).attr("price").toFixed(2)+'</td></tr>');
			Init_ProductClick();
		}else if(flag=="purchase")
		{
			var data = {};
			data.act = "Init_Library_Dialog";
			data.source=source;
			data.userid=userid;
			data.comid=comid;
			data.sign=GetSign(data.act);
			var str="";
			mui.getJSON(crm_domain, data, function (json) {
		            if (json.state) {
		            	$.each(json.data, function(i,info) {
		            		str+='<option value="'+info.ID+'">'+info.Name+'</option>';
		            	});
		            	$("#product_panel table").append('<tr id="'+o.attr("id")+'"><td width="20%" >'+o.find(".productname").html()+'</td><td width="20%"><select>'+str+'</select></td><td width="10%">'+o.attr("unit")+'</td><td width="15%"><div style="width:50px; height:100%; margin:0 auto;" class="ui-border-radius"><input type="tel" value="'+o.attr("price")+'" onkeyup="Check_Money(this);Cal_Purchase(this)" style="width:50px;" onfocus="ScrollTop();" onclick="Last(this);"/></div></td><td width="10%"><div style="width:30px; height:100%; margin:0 auto;" class="ui-border-radius"><input type="tel" value="1" onkeyup="Check_Money(this);Cal_Purchase(this)" style="width:30px;" onfocus="ScrollTop()" onclick="Last(this);"/></div></td><td width="10%"><div style="width:30px; height:100%; margin:0 auto;" class="ui-border-radius"><input type="tel" value="100" onkeyup="Check_Money(this);Cal_Purchase(this)" style="width:30px;" onfocus="ScrollTop()" onclick="Last(this);"/></div></td><td width="15%">'+o.attr("price").toFixed(2)+'</td></tr>');
		            	Init_ProductClick();
		            }
		   });

		}else if(flag=="movement")
		{
			if($("#product_panel table tr[id="+$(this).attr("id")+"]").length>0){
				plus.nativeUI.toast("已选择了该产品");
				return;
			}
			$("#product_panel table").append('<tr id="'+$(this).attr("id")+'"><td width="40%" >'+$(this).find(".productname").html()+'</td><td width="10%">'+$(this).attr("unit")+'</td><td width="18%">'+$(this).attr("price").toFixed(2)+'</td><td width="10%"><div style="width:40px; height:100%; margin:0 auto;" class="ui-border-radius"><input type="tel" value="1" onkeyup="Check_Money(this);Cal_Movement(this)" onfocus="ScrollTop()" onclick="Last(this);"/></div></td><td width="22%">'+$(this).attr("price").toFixed(2)+'</td></tr>');
			Init_ProductClick();
		}
		CloseWindow();
	});
};


//删除一项订单产品
function Delete_OrderDetail(o) {
    $(o).remove();
};


function Init_ProductClick(){
	
	mui("#product_panel").off("longtap","tr");
	mui("#product_panel").on("longtap","tr",function(){
		var o=this;
		plus.nativeUI.confirm("确定要删除产品【"+$(this).find("td")[0].innerHTML+"】吗？", function(event){
								if(event.index==1)
								{
									Delete_OrderDetail(o);
									if($("#product_panel table tr").length>0){
										$("#candelete").removeClass("hidden");
									}else
									{
										$("#candelete").addClass("hidden");
									}
								}
							},"提示", ["取消","确认"]);
	});
	if($("#product_panel table tr").length>0){
		$("#candelete").removeClass("hidden");
	}else
	{
		$("#candelete").addClass("hidden");
	}
	$("#contentlist2,#contentlist3").scrollTop(10000);
};
